package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.mysql.MysqlLink;
import com.ruoyi.system.domain.DabaTable;
import com.ruoyi.system.domain.DatabaseDetails;
import com.ruoyi.system.domain.DatabaseTable;
import com.ruoyi.system.service.IDabaTableService;
import com.ruoyi.system.service.IDatabaseDetailsService;
import com.ruoyi.system.service.IDatabaseTableService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DatabaseField;
import com.ruoyi.system.service.IDatabaseFieldService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 字段Controller
 * 
 * @author ruoyi
 * @date 2022-06-13
 */
@Controller
@RequestMapping("/system/field")
public class DatabaseFieldController extends BaseController
{
    private String prefix = "system/field";

    @Autowired
    private IDatabaseFieldService databaseFieldService;

    @Autowired
    private IDatabaseTableService databaseTableService;
    @Autowired
    private IDabaTableService dabaTableService;
    @Autowired
    private IDatabaseDetailsService databaseDetailsService;


    @GetMapping()
    public String field()
    {
        return prefix + "/field";
    }

    @GetMapping("/{tableId}")
    public String devicemanagement(ModelMap modelMap, @PathVariable("tableId") Long tableId,DatabaseField databaseField) {
        modelMap.put("tableId", tableId);
        try {
            databaseFieldService.deleteDatabaseFieldByTableId(tableId);
            DatabaseTable databaseTable = databaseTableService.selectDatabaseTableByTableId(tableId);
            DabaTable dabaTable = dabaTableService.selectDabaTableByDataId(databaseTable.getDataId());
            DatabaseDetails datails = databaseDetailsService.selectDatabaseDetailsByDatabaseId(dabaTable.getDatabaseId());
            List<Map<String,String>> list = MysqlLink.getAllField(datails.getIp(), datails.getPort(), datails.getRoot(), datails.getPassword(), dabaTable.getDataName(),databaseTable.getTableName());

            databaseField.setTableId(tableId);
            assert list != null;
            list.forEach(item -> {
              databaseField.setFieldName(item.get("name"));
              databaseField.setFieldType(item.get("type"));
              databaseFieldService.insertDatabaseField(databaseField);
            });
            sleep(200);
            return prefix + "/field";
        } catch (Exception e) {
            return prefix + "/errorfield";
        }
    }
    /**
     * 查询字段列表
     */
    @PostMapping("/list/{tableId}")
    @ResponseBody
    public TableDataInfo list( @PathVariable("tableId") Long tableId,DatabaseField databaseField)
    {
        startPage();
        List<DatabaseField> list = databaseFieldService.selectDatabaseFieldList(databaseField);
        return getDataTable(list);
    }

    /**
     * 选择字段导出数据
     */
    @PostMapping( "/generate")
    @ResponseBody
    public AjaxResult generate(String ids)
    {

        return AjaxResult.success();
    }

    /**
     * 导出字段列表 DabaTable dabaTable = new DabaTable();
     */
    @Log(title = "字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DatabaseField databaseField)
    {
        List<DatabaseField> list = databaseFieldService.selectDatabaseFieldList(databaseField);
        ExcelUtil<DatabaseField> util = new ExcelUtil<DatabaseField>(DatabaseField.class);
        return util.exportExcel(list, "字段数据");
    }

    /**
     * 新增字段
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存字段
     */
    @Log(title = "字段", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DatabaseField databaseField)
    {
        return toAjax(databaseFieldService.insertDatabaseField(databaseField));
    }

    /**
     * 修改字段
     */
    @GetMapping("/edit/{fieldId}")
    public String edit(@PathVariable("fieldId") Long fieldId, ModelMap mmap)
    {
        DatabaseField databaseField = databaseFieldService.selectDatabaseFieldByFieldId(fieldId);
        mmap.put("databaseField", databaseField);
        return prefix + "/edit";
    }

    /**
     * 修改保存字段
     */
    @Log(title = "字段", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DatabaseField databaseField)
    {
        return toAjax(databaseFieldService.updateDatabaseField(databaseField));
    }



    /**
     * 删除字段
     */
    @Log(title = "字段", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(databaseFieldService.deleteDatabaseFieldByFieldIds(ids));
    }
}
